package com.upf1sh.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.upf1sh.bean.AudioOrVideoEntity;
import com.upf1sh.mapper.AudioOrVideoMapper;
import com.upf1sh.service.AudioOrVideoService;
import com.upf1sh.utils.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.IOException;

@RestController
@CrossOrigin
@RequestMapping("/audioOrVideo")
public class AudioOrVideoController {
    @Resource
    private AudioOrVideoService audioOrVideoService;

    @Resource
    private AudioOrVideoMapper aud;

    @RequestMapping("/getVideo")
    public Result getVideo() {
        return Result.ok().data("videoList", audioOrVideoService.selectVideo());
    }

    @RequestMapping("/getAudioByPath")
    public Result getAudio(@RequestParam String path) {
        System.out.println("path" + path);
        return Result.ok().data("disPath", audioOrVideoService.selectAudioByPath(path).getWaveFormPath() == null ? "null" : audioOrVideoService.selectAudioByPath(path).getWaveFormPath());
    }

    @RequestMapping("/getAudio")
    public Result getAudioByPath() {
        return Result.ok().data("audioList", audioOrVideoService.selectAudio());
    }


    @PostMapping("/getAllAudioInDBByPage")
    public Result getAllAudio(@RequestParam(defaultValue = "1") int pageNum) {
        Page<AudioOrVideoEntity> page = new Page(pageNum, 5);
        Page<AudioOrVideoEntity> files = audioOrVideoService.findAllAudioInDBByPage(page);
        return Result.ok().data("items", files);
    }

    @PostMapping("/getAllVideoInDBByPage")
    public Result getAllVideo(@RequestParam(defaultValue = "1") int pageNum) {
        Page<AudioOrVideoEntity> page = new Page(pageNum, 5);
        Page<AudioOrVideoEntity> files = audioOrVideoService.findAllVideoInDBByPage(page);
        return Result.ok().data("items", files);
    }

    @RequestMapping("/saveVideo")
    public boolean saveVideo(MultipartFile file, String belong, String description) {
        return audioOrVideoService.saveVideo(file, belong, description);
    }

    @RequestMapping("/saveAudio")
    public boolean saveAudio(MultipartFile file, String belong, String description) throws UnsupportedAudioFileException, IOException {
        return audioOrVideoService.saveAudio(file, belong, description);
    }

    @RequestMapping("/insert")
    public int insert(@RequestBody AudioOrVideoEntity audioOrVideoEntity) {
//        System.out.println(audioOrVideoEntity);
        return aud.insert(audioOrVideoEntity);
    }

    @RequestMapping("/update")
    public Result update(@RequestBody JSONObject wave) {
        System.out.println("update" + wave.get("wavepath") + wave.get("path"));

        return Result.ok().data("status", audioOrVideoService.updateWave(wave.get("wavepath").toString(), wave.get("path").toString()));


    }
}